<template>
  <div class="com-container">
    <div
      class="com-charts"
      ref="rank_ref"
    >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      echartInstance: null, //echarts实例化对象
      allData: null, //服务器获取的所有数据
      startValue: 0, //区域缩放的起点值
      endValue: 9, //区域缩放的终点值
      timerId: null, //定时器标识
    };
  },
  mounted() {
    this.initecharts();
    this.getData();
    //屏幕大小监听事件
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    // 在组件销毁的时候, 需要将监听器取消掉
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    //1. 初始化echarts
    initecharts() {
      this.echartInstance = this.$echarts.init(this.$refs.rank_ref, "chalk"); //初始化

      //对echarts组件初始化
      const creatOption = {
        //标题
        title: {
          text: "▎地区销售排行",
          left: 30,
          top: 20,
        },

        //柱状体位置
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true, //包含坐标上的文字
        },
        //条目数据提示
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.echartInstance.setOption(creatOption);

      //鼠标移入停止定时器
      this.echartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      //鼠标移出开启定时器
      this.echartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    //2. 获取数据
    async getData() {
      const { data: ret } = await this.$axios.get("rank");
      this.allData = ret;
      //排序（从到小）
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });

      this.updateecharts(); //处理数据
      this.startInterval(); //定时器
    },
    //3. 更新echarts
    updateecharts() {
      //颜色渐变数组
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#12E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];

      //获取省份数据
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      //获取对应的销售金额
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              barBorderRadius: [33, 33, 0, 0], //设置圆角
              color: (arg) => {
                let targerColorArr = null;
                if (arg.value > 300) {
                  targerColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targerColorArr = colorArr[1];
                } else {
                  targerColorArr = colorArr[2];
                }

                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targerColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targerColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.echartInstance.setOption(dataOption);
    },
    //屏幕大小变化，完成自动适配器
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      // 和分辨率大小相关的配置项
      const adapterOption = {
          //标题
        title: {
          textStyle:{
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            barWidth:titleFontSize,  //柱条宽度
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0,0],  //设置圆角
            },
          },
        ],
      };

      this.echartInstance.setOption(adapterOption);
      // 手动的调用图表对象的resize 才能产生效果
      this.echartInstance.resize();
    },

    //定时器
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateecharts();
      }, 2000);
    },
  },
  components: {},
};
</script>

<style lang='' scoped>
</style>
